package com.xinchao.ck_mips.utils.task;

import com.orhanobut.logger.Logger;

import java.util.concurrent.BlockingQueue;

public class TaskDispatcher extends Thread {
    private volatile boolean mQuit = false;
    private BlockingQueue<Task> mQueue;

    public TaskDispatcher(BlockingQueue<Task> mQueue) {
        this.mQueue = mQueue;
    }

    public void quit() {
        mQuit = true;
        interrupt();
    }

    @Override
    public void run() {
        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
        Task task;
        while (true) {
            try {
                task = mQueue.take();
                if (task != null && !task.isRunning()) {
                    Logger.t(Thread.currentThread().getName()).d("TaskDispatcher start");
                    task.run();
                    Logger.t(Thread.currentThread().getName()).d("TaskDispatcher end");
                    sleep(20);
                }
            } catch (InterruptedException e) {
                if (mQuit) {
                    return;
                }
                continue;
            }

        }
    }

}

